package ysq.xyg.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.BaseConfig;
import ysq.xyg.model.info.BaseConfigInfo;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.BaseConfigService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;

/**
 * 系统配置
 *
 * @author congcong tao
 * @date 2018/10/15 10:18
 */
@Controller
@RequestMapping("/baseConfig")
public class BaseConfigController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(BaseConfigController.class);
    @Resource
    private BaseConfigService baseConfigService;
    private BaseConfig baseConfig;

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(){
        ModelAndView view = new ModelAndView("/baseConfigUpdateOrSave");
        baseConfig = baseConfigService.selectByPrimaryKey("1");
        view.addObject("baseConfig", baseConfig);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(BaseConfigInfo baseConfigInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(baseConfigInfo.getId())) {
            baseConfig = new BaseConfig();
            baseConfig.setId(BaseUtil.uId());
            baseConfig.setInBankDay(baseConfigInfo.getInBankDay());
            baseConfig.setRayaltyRate(baseConfigInfo.getRayaltyRate());
            baseConfig.setDistPriceOne(baseConfigInfo.getDistPriceOne());
            baseConfig.setDistPriceTwo(baseConfigInfo.getDistPriceTwo());
            baseConfig.setSubCommission(baseConfigInfo.getSubCommission());
            baseConfig.setMemberNotice(baseConfigInfo.getMemberNotice());
            int id = baseConfigService.insert(baseConfig);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            baseConfig = baseConfigService.selectByPrimaryKey(baseConfigInfo.getId());
            baseConfig.setInBankDay(baseConfigInfo.getInBankDay());
            baseConfig.setRayaltyRate(baseConfigInfo.getRayaltyRate());
            baseConfig.setDistPriceOne(baseConfigInfo.getDistPriceOne());
            baseConfig.setDistPriceTwo(baseConfigInfo.getDistPriceTwo());
            baseConfig.setSubCommission(baseConfigInfo.getSubCommission());
            baseConfig.setMemberNotice(baseConfigInfo.getMemberNotice());
            int id = baseConfigService.updateByPrimaryKey(baseConfig);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
}
